﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.BluingFilm
{
    [FlowAttributes(nameof(ModuleMotion), 4)]
    public class ModuleMotion : TaskBase
    {
        #region 轴变量
        private AxisMotion _axisX = default, _axisY = default, _axisZ = default, _axisR = default;
        #endregion

        #region 上升沿
        private bool _oneKeyHome = false;
        private string _pointname = default;
        #endregion

        #region 定时器
        private Ton _tonHome = new Ton();
        #endregion

        #region 点位
        private PointPositionInfoModel 拍照位;
        private PointPositionInfoModel 取料位1;
        private PointPositionInfoModel 取料位2;
        private PointPositionInfoModel 贴料位;
        #endregion

        #region 输入点
        private NormalOpen _机械手吸盘信号;
        private NormalOpen _撕膜无料检测光纤;
        private NormalOpen _物料吸盘检测光纤;
        private NormalOpen _物料到位光纤;
        #endregion

        #region 输出点
        private ICoil _移动物料吸盘真空阀;
        private ICoil _底座物料吸盘真空阀;
        #endregion

        #region 报警变量
        #endregion

        #region 私有变量
        #endregion
        protected ILog _log;
        public ModuleMotion(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _log = log;
            _移动物料吸盘真空阀 = iop.GetCoil("移动物料吸盘真空阀");
            _底座物料吸盘真空阀 = iop.GetCoil("底座物料吸盘真空阀");
            _撕膜无料检测光纤 = iop.GetNormalOpen("撕膜无料检测光纤");
            _物料吸盘检测光纤 = iop.GetNormalOpen("物料吸盘检测光纤");
            _物料到位光纤 = iop.GetNormalOpen("物料到位光纤");
            _机械手吸盘信号 = iop.GetNormalOpen("机械手吸盘信号");

            _axisX = axisp.GetAxisMotion("X轴");
            _axisY = axisp.GetAxisMotion("Y轴");
            _axisZ = axisp.GetAxisMotion("Z轴");
            _axisR = axisp.GetAxisMotion("R轴");

            拍照位 = axisp.GetPointPosition("拍照位");
            取料位1 = axisp.GetPointPosition("取料位1");
            取料位2 = axisp.GetPointPosition("取料位2");
            贴料位 = axisp.GetPointPosition("贴料位");
            _temporary.HomeComplete = false;
        }

        public override void EmergencyStop()
        {

        }

        private int _debug = 1;
        public override void Handle()
        {
            if (SignalOn(0))
            {
                switch (_debug)
                {
                    case 1:
                        NextStep();
                        break;
                    case 2:
                        NextStep(20);
                        break;
                }
            }
            if (SignalOn(10))
            {
                if (PointPositionMove(取料位1))
                {
                    NextStep(20);
                }
            }
            if (SignalOn(20))
            {
                if (PointPositionMove(取料位2))
                {
                    NextStep();
                }
            }
            if (SignalOn(30))
            {
                _axisZ.Move(取料位1.ZPulse, 10000);
                if (_axisZ.ArrivalLocation)
                {
                    NextStep();
                }
            }
            if (SignalOn(40))
            {
                _移动物料吸盘真空阀.Execute(true);
                NextStep();
            }
            if (SignalOn(50))
            {
                _axisZ.Move(0, 10000);
                if (_axisZ.ArrivalLocation)
                {
                    NextStep();
                }
            }
            if (SignalOn(60))
            {
                if (PointPositionMove(贴料位))
                {
                    NextStep();
                }
            }
            if (SignalOn(70))
            {
                _axisZ.Move(取料位1.ZPulse, 10000);
                if (_axisZ.ArrivalLocation)
                {
                    NextStep();
                }
            }
            if (SignalOn(80))
            {
                _移动物料吸盘真空阀.Execute(false);
                NextStep();
            }
            if (SignalOn(90))
            {
                _axisZ.Move(0, 10000);
                if (_axisZ.ArrivalLocation)
                {
                    _step = 0;
                    if (_debug == 1)
                    {
                        _debug = 2;
                    }
                    else
                    {
                        _debug = 1;
                    }
                }
            }
            if (_temporary.SystemStatus.ManualOperation)
            {
                #region 一键回原
                if (_temporary.OneKeyHome)
                {
                    _tonHome.Time(1000, true);
                    _oneKeyHome = _tonHome.Out;
                }
                else
                {
                    _tonHome.Time(1000, false);
                }
                if (_oneKeyHome)
                {
                    if (!_axisZ.HomeComplete)
                    {
                        _axisZ.Home();
                    }
                    if (_axisZ.HomeComplete)
                    {
                        if (!_axisX.HomeComplete) 
                            _axisX.Home();
                        if (!_axisY.HomeComplete)
                            _axisY.Home();
                        if (!_axisR.HomeComplete)
                            _axisR.Home();
                    }
                    if (_axisX.HomeComplete && _axisY.HomeComplete && _axisZ.HomeComplete && _axisR.HomeComplete)
                    {
                        _oneKeyHome = false;
                        _dialogHost.DialogShow("一键回原完成,允许开机！！！", Core.Enum.LogLevel.Success);
                    }
                }
                _temporary.HomeComplete = _axisX.HomeComplete && _axisY.HomeComplete && _axisZ.HomeComplete && _axisR.HomeComplete;
                #endregion
                #region 测试用纠偏

                #endregion
            }
        }

        public override void Resetting()
        {

        }

        public override void SafeStop()
        {

        }

        private bool PointPositionMove(PointPositionInfoModel point)
        {
            if (point == null)
            {
                return true;
            }
            _axisX.Move(point.XPulse, 10000);
            _axisY.Move(point.YPulse, 10000);
            _axisR.Move(point.RPulse, 10000);
            return _axisX.ArrivalLocation && _axisY.ArrivalLocation && _axisR.ArrivalLocation;
        }
    }
}
